package net.sk.china.platform.task.order;

import net.sk.china.platform.service.order.OrderTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 订单相关定时任务
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/14 21:24
 * +----------------------------------------------------------------------
 */

@Component
@EnableScheduling
public class StoreOrderTask {

    private static final Logger logger = LoggerFactory.getLogger(StoreOrderTask.class);

    private final OrderTaskService orderTaskService;

    public StoreOrderTask(OrderTaskService orderTaskService) {
        this.orderTaskService = orderTaskService;
    }

    @Async("taskExecutor")
    @Scheduled(fixedDelay = 1000 * 60L) //1分钟同步一次数据
    public void refund() {
        logger.info("执行用户退款申请任务");
        try {
            orderTaskService.refundApply();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("执行用户退款申请任务异常：" + e.getMessage());
        }
    }

    @Async("taskExecutor")
    @Scheduled(fixedDelay = 1000 * 60L)
    public void receiving() {
        logger.info("执行确认收货任务");
        try {
            orderTaskService.orderReceiving();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("执行确认收货异常：" + e.getMessage());
        }
    }

    @Async("taskExecutor")
    @Scheduled(fixedDelay = 1000 * 60L)
    public void complete() {
        logger.info("完成订单任务");
        try {
            orderTaskService.complete();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("完成订单异常：" + e.getMessage());
        }
    }


    @Async("taskExecutor")
    @Scheduled(fixedDelay = 1000 * 60L)
    public void cancelByUser() {
        logger.info("用户取消订单任务");
        try {
            orderTaskService.cancelByUser();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("用户取消订单异常：" + e.getMessage());
        }
    }

    @Async("taskExecutor")
    @Scheduled(fixedDelay = 1000 * 60L)
    public void autoComplete() {
        logger.info("订单自动完成任务");
        try {
            orderTaskService.autoComplete();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("订单自动完成异常：" + e.getMessage());
        }
    }

    @Async("taskExecutor")
    @Scheduled(fixedDelay = 1000 * 60L)
    public void autoCancel() {
        logger.info("自动取消未支付订单任务");
        try {
            orderTaskService.autoCancel();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("自动取消未支付订单异常：" + e.getMessage());
        }
    }
}
